package cz.hanis.android.geography;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cz.hanis.android.geography.models.Continent;
import cz.hanis.android.geography.models.Country;
import cz.hanis.android.geography.models.Test;
import cz.hanis.android.geography.xml.XMLParser;



import android.app.Application;
import android.content.res.AssetManager;
import android.graphics.drawable.Drawable;
import android.util.Log;

public class GeographyApplication extends Application {

	// private List<Country> countries;
	private static GeographyApplication INSTANCE;
	
	private static final String TAG = "GeographyApplication";
	private static final String TAG_ASSIGNING_VALUES = "ASSIGNING_VALUES(GeographyApplication)";

	private CountryTools countryTools;	
	private HighscoreHandler mHighscoreHandler;
	private TestManager mTestManager;

	@Override
	public void onCreate() {
		super.onCreate();
		INSTANCE = this;		
		XMLParser parser = new XMLParser();
		List<Country> countries = parser.parseCountries(this);
		List<Test> tests = parser.parseTests(this);		
		mTestManager = new TestManager(tests, this);
		countryTools = new CountryTools(countries);
		mHighscoreHandler = new HighscoreHandler(this);
		assingLocalizedValues();
	}
	
	public static GeographyApplication getInstance() {
		return INSTANCE;
	}

	public TestManager getTestManager() {
		return mTestManager;
	}

	public HighscoreHandler getHighScoreHandler() {
		return mHighscoreHandler;
	}
	
	@Override
	public void onTerminate() {
		super.onTerminate();
	}

	public CountryTools getCountryTool() {
		return countryTools;
	}

	public int getImageResource(String name) throws WrongResourceException {
		int resource = getResources().getIdentifier(name, "drawable",
				getPackageName());
		if (resource == 0) {
			throw new WrongResourceException("Image resource not found: "
					+ name);
		}
		return resource;
	}

	public int getStringResource(String name) throws WrongResourceException {
		int resource = getResources().getIdentifier(name, "string",
				getPackageName());
		if (resource == 0) {
			throw new WrongResourceException("String resource not found: "
					+ name);
		}
		return resource;
	}

	public String getStringValue(String name) throws WrongResourceException {
		return getString(getStringResource(name));
	}

	public Drawable getFlagDrawable(String id) throws WrongResourceException {
		AssetManager assets = getAssets();
		InputStream stream;
		try {
			stream = assets.open("images/flag/" + id.toLowerCase() + ".png");
		} catch (IOException e) {
			throw new WrongResourceException("Flag drawable not found: " + id);
		}
		return Drawable.createFromStream(stream, id);
	}
	
	public Drawable getLocationDrawable(String id) throws WrongResourceException {
		AssetManager assets = getAssets();
		InputStream stream;
		try {
			stream = assets.open("images/location/loc_" + id + ".png");
		} catch (IOException e) {
			throw new WrongResourceException("Location drawable not found: " + id);
		}
		return Drawable.createFromStream(stream, id);
	}

	public void assingLocalizedValues() {
		for (Test test : getTestManager().getTests()) {
			String id = test.getId();
			try {
				test.setName(getStringValue("test_name_" + id.toLowerCase()));
			} catch (WrongResourceException e) {
				Log.d(GeographyApplication.TAG_ASSIGNING_VALUES, e.getMessage()
						+ ", test name, id = " + id);
			}
			try {
				test.setDescription(getStringValue("test_description_" + id.toLowerCase()));
			} catch (WrongResourceException e) {
				Log.d(GeographyApplication.TAG_ASSIGNING_VALUES, e.getMessage()
						+ ", test description, id = " + id);
			}				
		}
		
		
		
		for (Country country : countryTools.getCountries(Continent.ALL)) {
			String id = country.getId();
			if (id == null || id.equals("")) {
				Log.d(GeographyApplication.TAG_ASSIGNING_VALUES, "wrong id");
				continue;
			}
			try {
				country.setName(getStringValue("country_" + id.toLowerCase()));
			} catch (WrongResourceException e) {
				Log.d(GeographyApplication.TAG_ASSIGNING_VALUES, e.getMessage()
						+ ", country, id = " + id);
			}

			try {
				country.setContinent(getStringValue("continent_" + country.getContinentId().toLowerCase()));
			} catch (WrongResourceException e) {
				Log.d(GeographyApplication.TAG_ASSIGNING_VALUES, e.getMessage()
						+ ", continent, id = " + id);
			}

			try {
				country.setCapital(getStringValue("capital_" + id.toLowerCase()));
			} catch (WrongResourceException e) {
				Log.d(GeographyApplication.TAG_ASSIGNING_VALUES, e.getMessage()
						+ ", capital, id = " + id);
			}
			
			for (String cId : country.getContiguousCountriesId()) {				
				try {
					Country cCountry = countryTools.getCountryById(cId);
					country.addContiguousCountry(cCountry);
				} catch (CountryNotFoundException e) {
					Log.d(TAG_ASSIGNING_VALUES, e.getMessage());
				}				
			}

//			try {
//				String region = country.getRegion();
//				if (region != null) {
//					country.setRegion(getStringValue(region));
//				} else {
//					Log.d(GeographyApplication.TAG_ASSIGNING_VALUES,
//							"region is null, id = " + id);
//				}
//
//			} catch (WrongResourceException e) {
//				Log.d(GeographyApplication.TAG_ASSIGNING_VALUES, e.getMessage()
//						+ ", region, id = " + id);
//			}
//
//			try {
//				country.setWikiLink(getStringValue("wiki_" + id));
//			} catch (WrongResourceException e) {
//				Log.d(GeographyApplication.TAG_ASSIGNING_VALUES, e.getMessage()
//						+ ", wiki, id = " + id);
//			}
//			List<String> languages = new ArrayList<String>();
//			for (String language : country.getLanguages()) {
//				try {
//					languages.add(getStringValue(language));
//				} catch (WrongResourceException e) {
//					Log.d(GeographyApplication.TAG_ASSIGNING_VALUES,
//							e.getMessage() + ", language, id = " + id);
//				}
//			}
//			country.setLanguages(languages);

//			try {
//				Currency currency = country.getCurrency();
//				if (currency != null) {
//					String code = country.getCurrency().getCode();
//					if (code == null) {
//						Log.d(GeographyApplication.TAG_ASSIGNING_VALUES,
//								"currency code is null, id = " + id);
//					} else {
//						code = code.toLowerCase();
//					}
//					country.getCurrency().setName(
//							getStringValue("currency_" + code));
//				} else {
//					Log.d(GeographyApplication.TAG_ASSIGNING_VALUES,
//							"currency is null, id = " + id);
//				}
//			} catch (WrongResourceException e) {
//				Log.d(GeographyApplication.TAG_ASSIGNING_VALUES, e.getMessage()
//						+ ", currencyName, id = " + id);
//			}
//
//			try {
//				String government = country.getGovernment();
//				if (government != null) {
//					country.setGovernment(getStringValue(government));
//				} else {
//					Log.d(GeographyApplication.TAG_ASSIGNING_VALUES,
//							"government is null, id = " + id);
//				}
//			} catch (WrongResourceException e) {
//				Log.d(GeographyApplication.TAG_ASSIGNING_VALUES, e.getMessage()
//						+ ", government, id = " + id);
//			}
		}
	}

}
